package com.example.poemtest.mine;

import android.content.Context;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.poemtest.R;
import com.example.poemtest.entity.PostInfoBean;
import com.example.poemtest.find.bean.CircleBean;
import com.example.poemtest.utils.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * created by JHT on 2024/5/14
 */
public class MyPostAdapter extends BaseAdapter {

    private Context mContext;
    private List<PostInfoBean.PostInfo> myPostList;
    private int layoutResId;

    public MyPostAdapter(Context mContext, List<PostInfoBean.PostInfo> myPostList, int layoutResId) {
        this.mContext = mContext;
        this.myPostList = myPostList;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        if (myPostList != null) {
            return myPostList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (myPostList != null) {
            return myPostList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHold;
        if (convertView == null) {
            //加载item的布局控件，生成一个View对象，再给convertView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取item中的控件对象
            viewHold = new ViewHold();
            viewHold.postContent = convertView.findViewById(R.id.poetry_content);
            viewHold.postDate = convertView.findViewById(R.id.poetry_title);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHold);
        } else {
            //重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的viewHolder对象
            viewHold = (ViewHold) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        viewHold.postContent.setText(myPostList.get(position).getPostContent());
//        viewHold.postDate.setText((CharSequence) myPostList.get(position).getPostCreateTime());
        return convertView;
    }

    private static class ViewHold {
        private TextView postContent, postDate;
    }
}
